package com.example.teacher_app.tch_activity.setting;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


import com.example.teacher_app.BuildConfig;
import com.example.teacher_app.R;
import com.example.teacher_app.data.TeacherData;
import com.example.teacher_app.databinding.ActivitySettingBinding;
import com.example.teacher_app.util.HttpUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class SettingActivity extends AppCompatActivity {

    private ActivitySettingBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.WhiteTheme);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.identity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View dialog = View.inflate(SettingActivity.this,R.layout.dialog_identity,null);
                EditText et_user = dialog.findViewById(R.id.et_user);
                EditText et_name = dialog.findViewById(R.id.et_name);
                EditText et_identity = dialog.findViewById(R.id.et_identity);
                Button btn_yes = dialog.findViewById(R.id.yes);
                Button btn_cancel = dialog.findViewById(R.id.cancel);
                final AlertDialog alertDialog = new AlertDialog.Builder(SettingActivity.this).setTitle("认证身份信息")
                        .setView(dialog)
                        .show();
                btn_yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String user = et_user.getText().toString();
                        String name = et_name.getText().toString();
                        String identity = et_identity.getText().toString();
                        if (user.isEmpty()){
                            Toast.makeText(SettingActivity.this, "账号不能为空！", Toast.LENGTH_SHORT).show();
                        }else if (name.isEmpty()){
                            Toast.makeText(SettingActivity.this, "姓名不能为空！", Toast.LENGTH_SHORT).show();
                        }else if (identity.isEmpty()){
                            Toast.makeText(SettingActivity.this, "身份证号不能为空！", Toast.LENGTH_SHORT).show();
                        }else {
                            identity();
                            alertDialog.dismiss();
                        }
                    }
                });
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
            }
        });

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        binding.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText et = new EditText(SettingActivity.this);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.setMargins(10, 2, 2, 10);
                et.setLayoutParams(lp);
                new AlertDialog.Builder(SettingActivity.this).setTitle("输入手机号")
                        .setView(et)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (et.getText().toString() == null){
                                    Toast.makeText(SettingActivity.this, "手机号码不能为空！", Toast.LENGTH_SHORT).show();
                                }else {
//                                    changePhone(et.getText().toString());
                                }

                            }
                        }).setNegativeButton("取消", null)
                        .show();
            }
        });

        binding.backLogin.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                ActivityManager activityManager = getSystemService(ActivityManager.class);
                List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
                for (ActivityManager.AppTask task : tasks) {
                    task.finishAndRemoveTask(); //销毁任务栈内全部Activity
                }
                ComponentName componentName = new ComponentName("com.ugrow.ugrowcommunity","com.ugrow.ugrowcommunity.LoginActivity");
                Intent intent = new Intent();
                intent.setComponent(componentName);
                startActivity(intent);
            }
        });
    }


    public void identity(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("id", TeacherData.TchNum);
        map.put("name",TeacherData.TeacherBean.getData().get(0).getTchName());
        map.put("tchCard",TeacherData.TeacherBean.getData().get(0).getTchCard());

        HttpUtil.httpPost(BuildConfig.TEACHER_CONFIRM, map, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
            @Override
            public void success(HttpUtil.Result result) {
                ConfirmBean confirmBean = new Gson().fromJson(result.content,ConfirmBean.class);
                Toast.makeText(SettingActivity.this,confirmBean.getData(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failed(IOException e) {
                e.printStackTrace();
                Toast.makeText(SettingActivity.this,e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        binding.imageView.setVisibility(View.GONE);
        binding.conLayout.setVisibility(View.VISIBLE);
        binding.conLayout.setEnabled(false);
        binding.identity.setEnabled(false);

    }

}